﻿using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Pages.Context;

namespace Rio.Framework.Pages.AppCode.Helpers
{

    public class Report
    {
        /// <summary>
        /// Generated a HTML formated string containing all informations about the folders
        /// </summary>
        /// <param name="p_lstFolders">List of all folders</param>
        /// <returns>A string HTML formated</returns>
        public static string FolderExport(List<Folder> p_lstFolders)
        {
            String l_sReturn = string.Empty;
            try
            {

                if (p_lstFolders != null && p_lstFolders.Count > 0)
                {
                    #region Headers

                    l_sReturn = GetFolderIndexValuesColumn(p_lstFolders[0]) + Environment.NewLine;
                    #endregion

                    foreach (Folder l_oFolder in p_lstFolders)
                    {
                        var query = GlobalWebContext.BusinessEntitiesDomainContext.AddActionHistoricQuery((int)Constant.UserActions.EXPORT_EXCEL, l_oFolder.FolderTypeIdentifier, l_oFolder.Identifier, null, null);
                        GlobalWebContext.BusinessEntitiesDomainContext.Load(query);
                        l_sReturn += GetFolderIndexValuesLine(l_oFolder) + Environment.NewLine; 
                    }
                }
            } 
            catch (Exception)
            {
                //Logger.Write("EXCEPTION : " + Environment.NewLine + ex, Logger.Level.Error);
                return null;
            }

            return l_sReturn;
        }

        /// <summary>
        /// Generated a HTML formated string containing all informations about the documents
        /// </summary>
        /// <param name="p_lstDocuments">List of all documents</param>
        /// <returns>A string HTML formated</returns>
        public static string DocumentExport(List<Document> p_lstDocuments)
        {
            String l_sReturn = string.Empty;
            try
            {
                if (p_lstDocuments != null && p_lstDocuments.Count > 0)
                {
                    #region Headers
                    l_sReturn = GetFolderIndexValuesColumn(p_lstDocuments[0].Folder) + Environment.NewLine;
                    #endregion

                    #region Data
                    string l_sValueOut = string.Empty;
                    foreach (Document l_oDocument in p_lstDocuments)
                    {
                        var query = GlobalWebContext.BusinessEntitiesDomainContext.AddActionHistoricQuery((int)Constant.UserActions.EXPORT_EXCEL, l_oDocument.Folder.FolderTypeIdentifier, l_oDocument.Folder.Identifier, l_oDocument.FolderIdentifier, l_oDocument.OriginalNameFile);
                        GlobalWebContext.BusinessEntitiesDomainContext.Load(query);
                        //l_sReturn += l_oDocument.Author + ";" + l_oDocument.OriginalNameFile + ";" + l_oDocument.PhotoCredit + ";" + l_oDocument.Note + ";" + Environment.NewLine;
                        l_sReturn += GetFolderIndexValuesLine(l_oDocument.Folder) + Environment.NewLine;
                    }

                    #endregion
                }
            }
            catch (Exception)
            {
                //Logger.Write("EXCEPTION : " + Environment.NewLine + ex, Logger.Level.Error);
                return null;
            }

            return l_sReturn;
        }

       /// <summary>
       /// Build the index label line
       /// </summary>
       /// <param name="p_oFolder"></param>
       /// <returns></returns>
        private static string GetFolderIndexValuesColumn(Folder p_oFolder)
        {
            string l_sReturn = string.Empty;
            foreach (Index l_oIndex in p_oFolder.ListIndex)
            {
                l_sReturn += string.Format("{0};", l_oIndex.IndexLabel);
            }
            return l_sReturn;
        }

        /// <summary>
        /// Build the index value line
        /// </summary>
        /// <param name="p_oFolder"></param>
        /// <returns></returns>
        private static string GetFolderIndexValuesLine(Folder p_oFolder)
        {
            string l_sReturn = string.Empty;
            string l_sDate = string.Empty;
            foreach (string l_sValue in p_oFolder.ListValues)
            {
                l_sDate = string.Empty;
                try
                {
                    DateTime l_oDateTimeTemp = DateTime.Parse(l_sValue);
                    l_sDate = l_oDateTimeTemp.Day.ToString() + "/" + l_oDateTimeTemp.Month.ToString() + "/" + l_oDateTimeTemp.Year.ToString();
                }
                catch { }

                if (l_sDate != string.Empty)
                    l_sReturn += string.Format("{0};", l_sDate);
                else
                    l_sReturn += string.Format("{0};", l_sValue);
            }
            return l_sReturn;
        }


    }
}
